package net.javalib.gvmax.post.server.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


public class VoicemailExtractor {
	
	/**
	 * Extract voice mail information.
	 * This includes phoneNumber, transcript, and media link
	 */
	public static Voicemail extractVoicemailInfo(MimeMessage message) throws IOException {
		Voicemail retVal = new Voicemail();
   		
		String vmText = MiscUtil.getEmailText(message);
		
		boolean capturingText = false;
   		BufferedReader reader = new BufferedReader(new StringReader(vmText));
   	
   		String line = reader.readLine();
   		while (line != null) {
   			line = line.trim();
   			
   			if (line.startsWith("You've got new voicemail from")) {
   				int bPos = line.indexOf('(');
   				int dPos = line.indexOf('-');
   				if (bPos == "You've got new voicemail from  ".length()) {
   					retVal.number = line.substring(bPos);
   				} else {
   					dPos = line.lastIndexOf('-');
       				retVal.number = line.substring(bPos,dPos);
   				}
   				retVal.number = MiscUtil.normalizeNumber(retVal.number.trim());
   			}
   			if (line.startsWith("Voicemail from ")) {
   				// Extract number
   				int bPos = line.indexOf("(");
   				if (bPos == -1) {
   					retVal.number = "unknown";
   				} else {
   					String number = line.substring(bPos);
   					number = number.substring(0,number.indexOf(" at "));
   					retVal.number = MiscUtil.normalizeNumber(number);
   				}
   			}
   			if (line.startsWith("Play message:")) {
   				// Grab link
   				capturingText = false;
   				retVal.link = reader.readLine();
   			}
   			if (capturingText) {
   				// Capturing transcript 2
   				retVal.transcript = retVal.transcript + line+"\n";
   			}
   			if (line.startsWith("Transcript: ")) {
   				// Capturing transcript 1
   				retVal.transcript = line.substring("Transcript: ".length())+"\n";
   				capturingText = true;
   			}
   			line = reader.readLine();
   		}
   		if (retVal.transcript != null && !"".equals(retVal.transcript))
   			retVal.transcript = retVal.transcript.substring(0,retVal.transcript.length()-1);
   		
   		if (!"unknown".equals(retVal.number)) {
   			try {
				retVal.contact = extractVoicemailContact(message.getSubject());
			} catch (MessagingException e) {
				e.printStackTrace();
			}
   		}
   		if (retVal.contact == null)
   			retVal.contact = retVal.number;
   		
   		try {
   			retVal.link = TinyURL.getTinyUrl(retVal.link);
   		} catch (Exception e) {
   			// ignore send long version
   		}
   		
   		return retVal;
	}

	/**
	 * Extract voice mail contact name from voicemail email subject line
	 */
	private static String extractVoicemailContact(String subject) {
		try {
			String from = subject.substring("New Voicemail from ".length());
			int bPos = from.lastIndexOf(" at ");
			if ( bPos != -1 ) {
				from = from.substring(0,bPos).trim();
				if (!"".equals(from))
					return from;
			}
			return null;
		} catch (RuntimeException e) {
			return null;
		}
	}

}
